//
//  ForecastSchedule.m
//  iMed
//
//  Created by Barry Press on 10/25/09.
//  Copyright 2009 Barry Press. All rights reserved.
//

#import "ForecastSchedule.h"
#import "MyDocument.h"


@implementation ForecastSchedule

#pragma mark Application methods
// =============================================================================
// ---------------------------------------------- createRXEventForRX:withDate --
- (RXEvent*)createRXEventForRX:(RX*)anRX withDate:(NSDate*)aDate
{
	RXEvent* rxe = [[[RXEvent alloc] init] autorelease];
	[rxe setTimestamp:aDate];
	[rxe setRx:anRX];
	[rxe setDose:[anRX dose]];
	[rxe setRxcount:[anRX perday]];
	return rxe;
}

// ---------------------------------------------- removeObjectsContainingRX: ---
- (void)removeObjectsContainingRX:(RX*)anRX
{
	BOOL bDone = NO;
	while (!bDone) {
		bDone = YES;
		for (int ix = 0; ix < [rxevents count]; ++ix) {
			if ([[rxevents objectAtIndex:ix] rx] == anRX) {
				bDone = NO;
				[rxevents removeObjectAtIndex:ix];
				break;
			}
		}
	}
}

// ---------------------------------------------- updateForecasts --------------
- (NSTimeInterval)updateForecastsReturningRXEvents:(NSArray**)pRXEvent
{
	NSTimeInterval nextInterval = FLT_MAX;
	NSMutableArray* aEvents = [[[NSMutableArray alloc] init] autorelease];
	
	for (int ix = 0; ix < [self count]; ++ix) {
		RXEvent* rxe = [self objectAtIndex:ix];
		NSTimeInterval intv = [rxe updateInterval];
		if ([[rxe rx] bAuto]) {
			nextInterval = fmin(nextInterval, intv);
			if (intv <= 0.0) {
				[aEvents addObject:rxe];
			}
		}
	}
	if (pRXEvent != NULL) {
		*pRXEvent = aEvents;
	}
	return nextInterval;
}

// ---------------------------------------------- textOutputScheduleToView: ----
- (void)textOutputScheduleToView:(NSView*)view withDataset:(DataSet*)ds
{
	for (int ix = 0; ix < [self count]; ++ix) {
		[[self objectAtIndex:ix] textOutputEventToView:view];
	}
}

#pragma mark Collection methods
// ---------------------------------------------- addObject --------------------
- (void)addObject:(RXEvent*)anObject
{
	NSAssert(rxevents != NULL, @"NULL array pointer in rxevents:addObject");
	[rxevents addObject:anObject];
}

// ---------------------------------------------- count ------------------------
- (int)count
{
	NSAssert(rxevents != NULL, @"NULL array pointer in rxevents:count");
	return [rxevents count];
}

// ---------------------------------------------- objectAtIndex ----------------
- (RXEvent*)objectAtIndex:(int)index
{
	NSAssert(rxevents != NULL, @"NULL array pointer in rxevents:objectAtIndex");
	return [rxevents objectAtIndex:index];
}

// ---------------------------------------------- removeAllObjects -------------
- (void)removeAllObjects
{
	NSAssert(rxevents != NULL, @"NULL array pointer in rxevents:removeAllObjects");
	[rxevents removeAllObjects];
}

// ---------------------------------------------- removeObject -----------------
- (void)removeObject:(RXEvent*)anObject
{
	NSAssert(rxevents != NULL, @"NULL array pointer in rxevents");
	[rxevents removeObject:anObject];
}

#pragma mark Archiving methods

// ---------------------------------------------- dealloc ----------------------
- (void)dealloc
{
	NSAssert(rxevents != NULL, @"NULL array pointer in rxevents:removeObject");
	[rxevents release];
	rxevents = NULL;
	[super dealloc];
}

// ---------------------------------------------- init -------------------------
- (id)init
{
	[super init];
	rxevents = [[[NSMutableArray alloc] init] retain];
	NSAssert(rxevents != NULL, @"NULL array pointer in rxevents:init");
	return self;
}

// ---------------------------------------------- encodeWithCoder --------------
- (void)encodeWithCoder:(NSCoder *)coder
{
	NSAssert(rxevents != NULL, @"NULL array pointer in rxevents:encodeWithCoder");
	[coder encodeObject:rxevents forKey:@"rxeventsArray"];
}

// ---------------------------------------------- initWithCoder ----------------
- (id)initWithCoder:(NSCoder *)coder
{
	[super init];
	rxevents = [[coder decodeObjectForKey:@"rxeventsArray"] retain];
	NSAssert(rxevents != NULL, @"NULL array pointer in rxevents:initWithCoder");
	return self;
}


@end
